/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.wallet.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.wallet.entity.LiquidationExpensesStatistics;
import com.pig4cloud.pigx.wallet.service.LiquidationExpensesStatisticsService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 强平清算费历史 控制器层
 *
 * @author pigx code generator
 * @date 2022-09-21 16:31:00
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/liquidationExpensesStatistics" )
@Api(value = "liquidationExpensesStatistics", tags = "强平清算费历史管理")
public class LiquidationExpensesStatisticsController {

    private final  LiquidationExpensesStatisticsService service;

    /**
     * 分页查询
     * @param page 分页对象
     * @param statistics
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R<Page<LiquidationExpensesStatistics>> getLiquidationExpensesStatisticsPage
	(Page<LiquidationExpensesStatistics> page, LiquidationExpensesStatistics statistics) {
		Page<LiquidationExpensesStatistics> statisticsPage = service.page(page, Wrappers.query(statistics)
				.orderByDesc("create_time"));
		return R.ok(statisticsPage);
    }


	/**
	 * 通过币对id查询强平清算费
	 * @return R
	 */
	@ApiOperation(value = "通过币对id查询强平清算费", notes = "通过币对id查询强平清算费")
	@GetMapping("/{symbolId}" )
	public R getById(@PathVariable("symbolId" ) Long symbolId) {
		LiquidationExpensesStatistics selectOne = service.getBaseMapper().selectOne(new QueryWrapper<LiquidationExpensesStatistics>() {{
			eq("symbol_id", symbolId);
			orderByDesc("create_time");
			last("limit 1");
		}});
		return R.ok(selectOne == null ? BigDecimal.ZERO : selectOne.getCost());
	}

    /**
     * 新增
     * @param liquidationExpensesStatistics 
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SysLog("新增" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('wallet_liquidationexpensesstatistics_add')" )
    public R save(@RequestBody LiquidationExpensesStatistics liquidationExpensesStatistics) {
        return R.ok(service.save(liquidationExpensesStatistics));
    }

    /**
     * 修改
     * @param liquidationExpensesStatistics 
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SysLog("修改" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('wallet_liquidationexpensesstatistics_edit')" )
    public R updateById(@RequestBody LiquidationExpensesStatistics liquidationExpensesStatistics) {
        return R.ok(service.updateById(liquidationExpensesStatistics));
    }

    /**
     * 通过id删除
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @SysLog("通过id删除" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('wallet_liquidationexpensesstatistics_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(service.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param liquidationExpensesStatistics 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('wallet_liquidationexpensesstatistics_export')" )
    public List<LiquidationExpensesStatistics> export(LiquidationExpensesStatistics liquidationExpensesStatistics) {
        return service.list(Wrappers.query(liquidationExpensesStatistics));
    }
}
